#!/bin/sh
###############################################################################
#
#  EGSnrc legacy script to submit parallel jobs
#  Copyright (C) 2015 National Research Council Canada
#
#  This file is part of EGSnrc.
#
#  EGSnrc is free software: you can redistribute it and/or modify it under
#  the terms of the GNU Affero General Public License as published by the
#  Free Software Foundation, either version 3 of the License, or (at your
#  option) any later version.
#
#  EGSnrc is distributed in the hope that it will be useful, but WITHOUT ANY
#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#  FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for
#  more details.
#
#  You should have received a copy of the GNU Affero General Public License
#  along with EGSnrc. If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################
#
#  Author:          Daryoush Sheikh-Bagheri, 1995
#
#  Contributors:    Blake Walters
#
###############################################################################
#
#  Submits parallel jobs using a batch system. Jobs submitted using pprocess
#  do not make use of the built-in parallel processing functionality of the
#  user codes. As such, it is really only useful if you do not have a C
#  compiler (required for built-in parallel functionality) or if your parallel
#  jobs each use separate pieces of a BEAMnrc phase space file.
#
###############################################################################


# Before anything else, make sure that the user has input numbers for the
# variables that have to be numbers.(P4,P6,P7)
if test ! -z "$4"		#true if $4 not vero length
  then
  test $4 -eq $4 2>/dev/null || echo "Invalid splitnum!"
  test $4 -eq $4 2>/dev/null || exit
fi
if test ! -z "$7"
  then
  test $7 -eq $7 2>/dev/null || echo "Invalid starting serial number!"
  test $7 -eq $7 2>/dev/null || exit
fi
if test ! -z "$8"
  then
  test $8 -eq $8 2>/dev/null || echo "Invalid sleep time!"
  test $8 -eq $8 2>/dev/null || exit
fi

echo "-----------------------------------------------------------------"
echo " Parallel processing on $my_machine"
echo "-----------------------------------------------------------------"

if test -z "$1"			#true if $1 has zero length, ie not input
 then
  echo " pprocess requires at least 3 and up to 9 inputs"
  echo ""
  echo " P1=  code (eg dosrznrc)"
  echo " P2=  ?.egsinp  "
  echo " P3=  ?.pegs4dat "
  echo " P4=  splitnum (defaults to 10)"
  echo " P5=  queue(short(20min) medium(2hr) long(40days--the default) "
  echo " P6=  batch (nqs, pbs, at--the default)"
  echo " P7=  _w? serial# to start (defaults to 1)"
  echo " P8=  sleep time between job submissions (default=1s)"
  echo " P9=  (optional) phase space source (no extension) of which P4 pieces exist"
  exit
fi



if test $1 = "help"
 then
  echo " pprocess requires at least 3 and up to 9 inputs"
  echo ""
  echo " P1=  code (eg dosrznrc)"
  echo " P2=  ?.egsinp "
  echo " P3=  ?.pegs4dat "
  echo " P4=  splitnum (defaults to 10)"
  echo " P5=  queue(short(20min) medium(2hr) long(40days--the default) "
  echo " P6=  batch (nqs, pbs, at--the default)"
  echo " P7=  _w? serial# to start (defaults to 1)"
  echo " P8=  sleep time between job submissions (default=1s)"
  echo " P9=  (optional) phase space source (no extension) of which P4 pieces exist"
  exit
fi


echo "--------------------------------------------------------------------------"
echo "P1=$1 P2=$2 P3=$3 P4=$4 "
echo "P5=$5 P6=$6 P7=$7 P8=$8 P9=$9"
echo " "
echo "user code is $1:    input file is $2.egsinp"
echo "PEGS data is $3.pegs4dat"
if [ "$4" = "" ]
        then
        echo "No. of machines run is farmed out to:     10"
else
        echo "No. of machines run is farmed out to:     $4 "
fi
if [ "$5" = "" ]
	then
	echo "Que used is  long"
else
	echo "Que used is $5 "
fi
if [ "$6" = "" ]
        then
        echo "batch used is at"
else
        echo "batch used is $6 "
fi
if [ "$7" = "" ]
	then
	echo "Automatic names start with _w1"
else
	echo "Automatic names start with _w$7 "
fi
if [ "$8" = "" ]
        then
        echo "Sleep time between job submissions = 1s"
else
        echo "Sleep time between job submissions = $7s"
fi
if [ "$9" != "" ]
        then
        echo "Will use pieces of phase space source $9.egsphsp#"
        echo "Pieces will not be partitioned."
fi
echo " "
echo "--------------------------------------------------------------------------"
if test -z "$4"
then
 howmany=10
else
 howmany=$4
fi

if test -z "$7"
then
 p=1
else
 p=$7
fi

porig=$p

if test -z "$5"
then
 q="long"
else
 q=$5
fi

if test -z "$8"
then
 s=1
else
 s=$8
fi

batch="unknown"

if test -z "$6"
then
 batch="at"
fi
if test "$6" = "nqs"
 then
 batch="nqs"
fi
if test "$6" = "NQS"
 then
 batch="nqs"
fi
if test "$6" = "pbs"
 then
 batch="pbs"
fi
if test "$6" = "PBS"
 then
 batch="pbs"
fi
if test "$6" = "at"
 then
 batch="at"
fi
if test "$6" = "AT"
 then
 batch="at"
fi
if test "$6" = "unknown"
 then
 echo "Batch type $6 is not recognized.  Try again."
 exit
fi

if test ! -f $2.egsinp
then
 echo "input file $2.egsinp does not exist."
 echo "Check you are on the right area. Current area is:"
 pwd
 exit
fi

if test ! -f $HEN_HOUSE/pegs4/data/$3.pegs4dat
then
 if test ! -f $EGS_HOME/pegs4/data/$3.pegs4dat
    then
    echo "PEGS4 data file $3.pegs4dat does not exist on "
    echo "$HEN_HOUSE/pegs4/data nor $EGS_HOME/pegs4/data"
    exit
 fi
fi


#$HEN_HOUSE/pprocess/pprocess_${my_machine} $2 $howmany $1 $3 $q $p $8
#above is old command

#below was in pprocess_Linux
cd $EGS_HOME/$1

roof=`expr $howmany + $p`

itera=0

while
 test $p -lt $roof
do
 if test -f $2_w$p.egsinp
  then
  /bin/rm $2_w$p.egsinp
 fi

awk -v "nline=`cat $2.egsinp | wc -l`" -v code=$1 -v divisions=$howmany -v random=$p -v ipar=$howmany -v parnum=`expr $itera + 1` -v phspfile=$9 'BEGIN {line = 0; writenow = 0; FS = ","; nmed = 0; sourceline = 0}

{

line ++
if ( code == "dosxyznrc" ) {
    if (writenow == 1) print $0
    while (writenow != 1) {
        if (line == 1) {
           nextline = $0
        }
        numfields = split(nextline,data,",")
        if (line == 2) {nmed = data[1];}
        if (nmed > 0 && line == nmed + 4 && data[4] == 1 && parnum > 1) {
            nextline = data[1]","data[2]","data[3]", 0"
        }
        oldline = nextline
        getline nextline
        line ++
        if (index(nextline,"#") > 0 || line == nline) {
          histtot=data[1]; histi= int(histtot /divisions) ;
          if (data[4] == 0) {data[4] = 97;}
          if (data[5] == 0) {data[5] = 33;}
          if (phspfile == "") {
            printf("%i,%i,%6.1f,%i,%i,%6.1f,%i,%i,%i,%i,%6.2f,%i,%i,%i,%i\n",
                histi,data[2],data[3],data[4],data[5] + parnum -1,
            data[6],data[7],data[8],data[9],data[10],data[11],data[12],
            ipar,parnum,data[15])
          }
          else {
            printf("%i,%i,%6.1f,%i,%i,%6.1f,%i,%i,%i,%i,%6.2f,%i,%i,%i,%i\n",
                histi,data[2],data[3],data[4],data[5] + parnum -1,
            data[6],data[7],data[8],data[9],data[10],data[11],data[12],
            ipar,0,data[15])
          }
          print nextline
          writenow = 1
        }
        else {
          print oldline
        }
   }
}
else if ( match(code,/BEAM/) > 0 ) {
    if (line != 4 && line != sourceline) {print $0;}
    if (line == 4) {
           histtot=$1; histi= int(histtot /divisions) ;
           if ($2 == 0) {$2 = 97;}
           if ($3 == 0) {$3 = 33;}
           printf("%i,%i,%i,%6.1f,%i,%i,%1i,%s,%s\n",histi,
           $2,$3 + parnum -1,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16)
           if ($5 == 29 && $8 > 0) {
               sourceline = 7
           }
           else if ($5 == 29 && $8 == 0) {
               sourceline = 6
           }
           else if ($5 != 29 && $8 > 0) {
               sourceline = 6
           }
           else {
               sourceline = 5
           }
    }
    if (line == sourceline) {
           if ($2 == 21) {
             if (phspfile == "") {
               printf("%i,%i,%i,%i,%i,%i,%s,%s\n",$1,$2,$3,$4,ipar,parnum,$7,$8)
             }
             else {
               printf("%i,%i,%i,%i,%i,%i,%s,%s\n",$1,$2,$3,$4,ipar,0,$7,$8)
             }
           }
           else {
              print $0
           }
    }
}
else {
       FS = " "
       if ($3 == "HISTORIES=") {
                print $1, $2, $3, int($4/divisions)
       }
       else if ($4 == "SEEDS=") {
                print $1, $2, $3, $4, $5, $6+parnum-1
       }
       else if ($1 == "SOURCE" && $2 == "OPTIONS=") {
                if (phspfile == "") {
                  partemp = parnum
                }
                else {
                  partemp = 0
                }
                if (isource == 21) {
                    printf("%s %s %i, %i, %i, %i\n",$1,$2,$3,$4,ipar,partemp)
                }
                else if (isource == 22) {
                  if (index($6,",") > 0) {
                    printf("%s %s %s %s %s %s %i, %i, %i\n",$1,$2,$3,$4,
                    $5,$6,$7,ipar,partemp)
                  }
                  else {
                    printf("%s %s %s %s %s %s, %i, %i, %i\n",$1,$2,$3,$4,
                    $5,$6,$7,ipar,partemp)
                  }
                }
                else if (srctypread == 1) {
                    print $0
                }
                else {
                    srcoptread = 1
                    for (i = 1; i <= 9; i++)
                          data[i] = $i
                }
       }
       else {
                print $0
       }
       if ($1 == "SOURCE" && $2 == "NUMBER=") {
                isource = $3
                srctypread = 1
                if (srcoptread == 1) {
                    if (isource == 21) {
                       printf("%s %s %i, %i, %i, %i\n",data[1],data[2],data[3],
                       data[4],ipar,partemp)
                    }
                    else if (isource == 22) {
                      if ( index(data[6],",") > 0) {
                       printf("%s %s %s %s %s %s %i, %i, %i\n",data[1],
                       data[2],data[3],data[4],data[5],data[6],data[7],
                       ipar,partemp)
                      }
                      else {
                       printf("%s %s %s %s %s %s, %i, %i, %i\n",data[1],
                       data[2],data[3],data[4],data[5],data[6],data[7],
                       ipar,partemp)
                      }
                    }
                    else {
                        printf("%s %s %s %s %s %s\n",data[1],data[2],data[3],
                       data[4],data[5],data[6])
                    }
                }
       }
}
   }'  $2.egsinp > $2_w$p.egsinp

itera=`expr $itera + 1`

if test $itera -eq 1
 then
 echo $2_w$p > $2.parallelfiles
else
   echo $2_w$p >> $2.parallelfiles
fi

p=`expr $p + 1`

done


 /bin/rm 13

p=$porig

while
  read pcf
do
  echo -e "$pcf  \t \c" >> 13
#echo "$pcf  \c"
done < $2.parallelfiles

#end of what was in pprocess_Linux

read pcfiles < 13


for pcfile in $pcfiles

do
  echo $pcfile

  if test ! -z "$9"
  then
     /bin/cp $pcfile.egsinp  tempa
     sed -e "s/$9/$9_w$p/g" tempa > $pcfile.egsinp
  fi

  p=`expr $p + 1`

  $HEN_HOUSE/scripts/run_user_code_batch $1 $pcfile $3 $q batch=$batch

  sleep $s

done

exit
